//*************************************************************************************
//* 作    者： zouhunter
//* 创建时间： 2021-09-18 02:13:36
//* 描    述： 挥舞音效

//* ************************************************************************************
using UnityEngine;

public class WaveAudioBehaviour : MonoBehaviour {
    [SerializeField] float smoothing = 5f;     //Amount of smoothing applied to the change in the audio
    [SerializeField] float minSpeed = 7f;       //The minimum speed required for audio
    [SerializeField] float maxSpeed = 11f;      //The speed required for maximum audio
    [SerializeField] float minVolume = 0f;      //Minimum volume
    [SerializeField] float maxVolume = .6f;     //Maximum volume
    [SerializeField] float minPitch = .3f;      //Minimum pitch
    [SerializeField] float maxPitch = 1f;       //Maximum pitch

    public bool Calcute(float speed,ref float pitch,ref float volume)
    {
        //Get the current speed of the sword
        float currentSpeed = speed;

        //If the speed is below the required speed and the audio isn't active, exit
        if (currentSpeed < minSpeed)
            return false;

        //Calculate the percentage the current speed is between the minimum and maximum
        float percentage = Mathf.Clamp((currentSpeed - minSpeed) / (maxSpeed - minSpeed), 0f, 1f);

        //Calculate the desired volume and pitch based on the speed percentage
        float targetPitch = Mathf.Lerp(minPitch, maxPitch, percentage);
        float targetVolume = Mathf.Lerp(minVolume, maxVolume, percentage);

        //Lerp the current volume and pitch based on the desired values
        float appliedPitch = Mathf.Lerp(pitch, targetPitch, smoothing * Time.deltaTime);
        float appliedVolume = Mathf.Lerp(volume, targetVolume, smoothing * Time.deltaTime);

        //If the volume is below a very small amount, apply it
        if (appliedVolume >= .01f)
        {
             pitch = appliedPitch;
             volume = appliedVolume;
             return true;
        }
        //...otherwise set the values to their minimum and set the audio to inactive
        else
        {
            pitch = minPitch;
            volume = minVolume;
            return false;
        }
    }
}
